package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.DialogUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.google.gson.reflect.TypeToken;

/**
 * 登陆
 * 
 * @author G
 * 
 */
public class LoginActivity extends BaseActivity {

	private EditText et_denglu;
	private EditText et_denglumima;

	private TextView btn_login, tv_forget_password;

	// ///////////////////////////////////////////////////////////////////////////跳转到注册也可以返回，若注册成功则关闭当前页
	private Handler loginHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			DialogUtil.endProgressDialog(mDialog);
			String dataStr = data.getString(HttpCons.DATA);
			if (state.equals("001")) {
				MyConst.setACCOUNT("p_" + et_denglu.getText().toString());
				MyConst.setUserInfo(et_denglumima.getText().toString(),
						et_denglu.getText().toString());
				Map<String, String> map = HttpServer.gson.fromJson(dataStr,
						new TypeToken<Map<String, String>>() {
						}.getType());
				if (map.containsKey("token")) {
					MyConst.TOKEN = map.get("token");
				} else {
					MyConst.TOKEN = "";
				}

				// if (map.get("isComplete").equals("0")) {
				Toast.makeText(getApplicationContext(), "登录成功", 0).show();
				// 初始化xmpp
				MyConst.isLoginXmpp = 0;

				if (map.get("isComplete").equals("1")) {

					Intent mianIntent = new Intent(LoginActivity.this,
							CenterInfoChangeActivity.class);
					mianIntent.putExtra("isComplete", 0);
					startActivity(mianIntent);
					finish();
				} else {

					Intent mianIntent = new Intent(LoginActivity.this,
							MainActivity.class);
					startActivity(mianIntent);
					finish();
				}

			} else {
				Toast.makeText(getApplicationContext(), msg, 0).show();
				btn_login.setOnClickListener(LoginActivity.this);
				btn_login.setClickable(true);
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			DialogUtil.endProgressDialog(mDialog);
			btn_login.setOnClickListener(LoginActivity.this);
			btn_login.setClickable(true);
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(LoginActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)
			}
		}
	};
	private TextView tv_forget_zhuce;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		initView();

	}

	private void initView() {
		initTitleBar();
		tv_forget_zhuce = (TextView) findViewById(R.id.tv_forget_zhuce);
		tv_forget_zhuce.setOnClickListener(this);
		et_denglu = (EditText) findViewById(R.id.et_denglu);
		et_denglumima = (EditText) findViewById(R.id.et_denglumima);
		btn_login = (TextView) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(this);
		tv_forget_password = (TextView) findViewById(R.id.tv_forget_password);
		tv_forget_password.setOnClickListener(this);
		String loginName = MyConst.getUserinfo().get("loginName");
		if (!TextUtils.isEmpty(loginName)) {
			et_denglu.setText(loginName);
		}

	}

	private void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);

		tv_title_title.setText(getString(R.string.login));

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			MyConst.setACCOUNT("");
			finish();
		}
		return true;
	}

	private AlertDialog mDialog;

	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent();
		switch (arg0.getId()) {
		case R.id.tv_forget_zhuce:
			intent.setClass(LoginActivity.this, RegisterActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_login:
			btn_login.setOnClickListener(LoginActivity.this);
			btn_login.setClickable(false);
			if (TextUtils.isEmpty(et_denglu.getText().toString())) {
				btn_login.setOnClickListener(LoginActivity.this);
				btn_login.setClickable(true);
				Toast.makeText(getApplicationContext(), MyConst.DENG_LU, 0)
						.show();
			} else if (TextUtils.isEmpty(et_denglumima.getText().toString())) {
				btn_login.setOnClickListener(LoginActivity.this);
				btn_login.setClickable(true);
				Toast.makeText(getApplicationContext(), "密码不能为空", 0).show();
			} else {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("channelIds", MyConst.CHANNELID);
				TransferParameter parameter = new TransferParameter();

				parameter.setUsername("p_" + et_denglu.getText().toString());
				parameter.setPassword(et_denglumima.getText().toString());
				parameter.setData(map);
				new HttpServer().post(HttpCons.LOGIN, parameter, loginHandler);
				// 第二个参数为显示的文字 null或""显示默认值"正在努力为您加载..."
				mDialog = DialogUtil.startProgressDialog(this, "正在加载......");
			}

			break;
		case R.id.tv_forget_password:
			Intent passwordfortIntent = new Intent(LoginActivity.this,
					PasswordBackActivity.class);
			startActivity(passwordfortIntent);
			break;
		default:
			break;
		}

	}
}
